//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: SymbolNew_cb.c 757 2007-05-10 08:19:31Z roger $
//

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>
#include <glade/glade.h>
#include <gconf/gconf-client.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "prefedit.h"
#include "SymbolNew_cb.h"
#include "list_ctrl.h"
#include "list_factory.h"
#include "dialog.h"

void
on_Symbol_Kind_Tree_View_Selection_changed (GtkTreeSelection *selection,
		gpointer data)
{
	GtkWidget *w;
	GtkTreeIter iter;
	GtkTreeModel *model;
	gint idx = 0;

	if (!sym_sel)
		g_free(sym_sel);
	sym_sel = NULL;

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolCands");

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &idx,
				-1);
		Symbol_Cands_refresh_treeview(GTK_TREE_VIEW(w), idx);
	}
}

void
on_Symbol_Cands_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	GtkTreeIter iter;
	GtkTreeModel *model;
	
	if (!sym_sel)
		g_free(sym_sel);
	sym_sel = NULL;

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_1, &sym_sel,
				-1);
	}
}

void
on_Key_Cands_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	GtkTreeIter iter;
	GtkTreeModel *model;

	if (!key_sel)
		g_free(key_sel);
	key_sel = NULL;

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_1, &key_sel,
				-1);
	}
}


void
on_Symbol_new_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	return;
}

void
on_Symbol_new_Button_OK_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GConfClient *client = gconf_client_get_default();

	if (!symbol_list_new) {
		w = glade_xml_get_widget(xml_SymbolNew,
				"windowSymbolNew");
		gtk_widget_destroy(w);
		return;
	}
	gconf_client_set_int(client,
			PREFEDIT_COMMAND_KEY,
			REFRESH_SYMBOL_LIST,
			NULL);

	w = glade_xml_get_widget(xml_SymbolNew, "windowSymbolNew");
	g_object_unref(G_OBJECT(xml_SymbolNew));
	gtk_widget_destroy(w);
}

void
on_Symbol_new_Button_Cancel_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;

	w = glade_xml_get_widget(xml_SymbolNew, "windowSymbolNew");
	g_object_unref(G_OBJECT(xml_SymbolNew));
	gtk_widget_destroy(w);
}

void
on_Symbol_new_Button_Add_clicked (GtkButton *button, gpointer data)
{
	GtkWidget *w;
	gint modifier = 0;
	SymDefGroup *pSDG = NULL;
	GList *l = NULL;

	if (sym_sel == NULL) {
		warning_dialog(_("Please choose symbol you want to define!"));
		return;
	}

	if (key_sel == NULL) {
		warning_dialog(_("Please choose key you want to use!\n"));
		return;
	}

	pSDG = g_new0(SymDefGroup, 1);

	w = glade_xml_get_widget(xml_SymbolNew, "checkbuttonCtrl");
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
		modifier |= IM_CTRL_MASK;

	w = glade_xml_get_widget(xml_SymbolNew, "checkbuttonShift");
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
		modifier |= IM_SHIFT_MASK;

	w = glade_xml_get_widget(xml_SymbolNew, "checkbuttonAlt");
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
		modifier |= IM_ALT_MASK;

	if (!modifier) {
		warning_dialog(_("Please choose a mask key!\n"));
		return;
	}

	pSDG->symbol = g_strdup(sym_sel);
	pSDG->modifier = modifier;
	pSDG->key = IM_VK_A + key_sel[0] - 'A';
	l = symbol_list;
	while (l) {
		SymDefGroup *p = (SymDefGroup *)l->data;
		if ((p->modifier == pSDG->modifier) &&
				(p->key == pSDG->key)) {
			warning_dialog("This key group had been defined!");
			return;
		}
		l = g_list_next(l);
	}
	l = symbol_list_new;
	while (l) {
		SymDefGroup *p = (SymDefGroup *)l->data;
		if ((p->modifier == pSDG->modifier) &&
				(p->key == pSDG->key)) {
			warning_dialog("This key group had been defined!");
			return;
		}
		l = g_list_next(l);
	}
	symbol_list_new = g_list_append(symbol_list_new, pSDG);

	w = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolNew");
	Symbol_new_refresh_treeview_by_symbol_list(GTK_TREE_VIEW(w),
			symbol_list_new);
}

void
on_Symbol_new_Button_Del_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *treeview;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GtkTreeModel *model;
	GList *l = NULL;
	gint idx;

	treeview = glade_xml_get_widget(xml_SymbolNew, "treeviewSymbolNew");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_2, &idx,
				-1);
	}
	else {
		warning_dialog(_("Please choose Symbol you want to delete!"));
		return;
	}

	l = g_list_nth(symbol_list_new, idx);
	if (l->next)
		l->next->prev = l->prev;
	if (l->prev)
		l->prev->next = l->next;
	else
		symbol_list_new = l->next;
	if (symbol_list_new)
		symbol_list_new->prev = NULL;
	l->next = NULL;
	l->prev = NULL;
	g_free(((SymDefGroup *)l->data)->symbol);
	g_list_free(l);
	Symbol_new_refresh_treeview_by_symbol_list(GTK_TREE_VIEW(treeview),
			symbol_list_new);
}

void
on_Symbol_new_Window_destroy(GtkWidget *widget, gpointer data)
{
	g_object_unref(G_OBJECT(xml_SymbolNew));
	gtk_widget_destroy(widget);
}

// vi:ts=4:nowrap:cin:expandtab
